package com.ym.script;

import java.io.File;
import java.util.HashMap;

import com.google.gson.Gson;
import com.ym.script.util.FileUtil;

public class Config {
	public static final String SCRIPT_VIEW_URL = "view.txt";
	public static final String SCRIPT_CLASS = "com.ym.script.Script";
	public static String SCRIPT_PATH = "";
	private HashMap<String, Object> map = new HashMap<String, Object>();

	public HashMap<String, Object> getMap() {
		return map;
	}

	public void setMap(HashMap<String, Object> map) {
		this.map = map;
	}

	private static Config config;

	public static void save() {
		if (config == null) {
			return;
		}
		String data = new Gson().toJson(config);
		FileUtil.write(new File(SCRIPT_PATH, ".config"), data);
	}

	public static void read() {
		if (config != null) {
			return;
		}
		try {
			config = new Gson().fromJson(
					FileUtil.readAllText(new File(SCRIPT_PATH, ".config")),
					Config.class);
		} catch (Exception e) {
		}
		if (config == null) {
			config = new Config();
		}
	}

	public static synchronized void put(String key, Object value) {
		read();
		config.getMap().put(key, value);
		save();
	}

	public static synchronized Object get(String key) {
		read();
		return config.getMap().get(key);
	}
}
